过滤器
使用@WebFilter
创建:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @WebFilter(filterName = "MyFilter1", urlPatterns = "/*") @Slf4j public class MyFilter1 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("MyFilter1 初始化"); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("MyFilter1 过滤操作"); filterChain.doFilter(servletRequest, servletResponse); }
@Override public void destroy() { log.info("MyFilter1 销毁"); } }
|
该方法创建完过滤器后,需要在DemoApplication
类加上@ServletComponentScan
注解,让SpringBoot扫描到。
@Slf4j
为lombok注解,具体可百度。
启动项目,访问任意url,可以看日志输出发现我们的过滤器已生效。
@Bean
方式创建
创建一个filter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Slf4j public class MyFilter2 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("MyFilter2 初始化"); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("MyFilter2 过滤操作"); filterChain.doFilter(servletRequest, servletResponse); }
@Override public void destroy() { log.info("MyFilter2 销毁"); } }
|
新建一个FilterConfig类:
1 2 3 4 5 6 7 8 9 10
| @Configuration public class FilterConfig { @Bean public FilterRegistrationBean myFilter2Register() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new MyFilter2()); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } }
|
该方式不需要在DemoApplication
类加上@ServletComponentScan
注解。
启动项目,访问任意url,可以看日志输出发现我们的过滤器已生效。
监听器
和过滤器差不多,也有两种创建方式。
使用@WebListener
方式创建
1 2 3 4 5 6 7 8 9 10 11 12 13
| @WebListener @Slf4j public class MyListener1 implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { log.info("MyListener1 初始化"); }
@Override public void contextDestroyed(ServletContextEvent servletContextEvent) { log.info("MyListener1 销毁"); } }
|
该方法创建完后,需要在DemoApplication
类加上@ServletComponentScan
注解,让SpringBoot扫描到。
启动项目查看日志输出,发现监听器已生效。
@Bean
方式创建
创建一个listener类:
1 2 3 4 5 6 7 8 9 10 11 12
| @Slf4j public class MyListener2 implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { log.info("MyListener2 初始化"); }
@Override public void contextDestroyed(ServletContextEvent servletContextEvent) { log.info("MyListener2 销毁"); } }
|
创建一个ListenerConfig类:
1 2 3 4 5 6 7 8 9
| @Configuration public class ListenerConfig { @Bean public ServletListenerRegistrationBean myListener2Register() { ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); bean.setListener(new MyListener2()); return bean; } }
|
该方式不需要在DemoApplication
类加上@ServletComponentScan
注解。
启动项目,查看日志输出发现监听器已生效。
拦截器
新建一个类继承HandlerInterceptorAdapter
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Slf4j public class MyInterceptor1 extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info("MyInterceptor1 preHandle 请求处理前调用"); return true; }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info("MyInterceptor1 postHandle 请求处理后,视图渲染前调用"); }
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info("MyInterceptor1 afterCompletion 请求结束后调用"); } }
|
新建一个类实现WebMvcConfigurer
:
1 2 3 4 5 6 7 8 9 10
| @Configuration public class InterceptorConfig implements WebMvcConfigurer {
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/home").excludePathPatterns("/"); } }
|
注意,因为Java8中WebMvcConfigurer
提供了default methods,所以可以直接实现WebMvcConfigurer
类。在SpringBoot 2.0之前,一般通过继承WebMvcConfigurerAdapter
类来创建。
好了,启动项目,访问http://localhost:8080/home ,查看日志输出发现拦截器已经生效了。